package com.base.user.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mysteel.util.ObjectUtils;

import com.base.data.exception.DataException;
import com.base.user.dao.EmployeeDao;
import com.base.user.entity.Employee;
import com.mysteel.page.Page;
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService
{
	@Autowired
	private EmployeeDao employeeDao;

	@Override
	public void saveEmployee(Employee employee)
	{
		if(ObjectUtils.notNull (employee)){
			
			Map<String,Object> params = new HashMap<String,Object>();
			
			params.put ("name" ,employee.getName ());
			params.put ("enName" ,employee.getEnName  ());
			params.put ("departmentId" ,employee.getDepartmentId ());
			
			if(ObjectUtils.isEmpty  (employeeDao.query (params)))
			{
				
				employee.setCreateTime(System.currentTimeMillis ());
				employee.setLastAccess (System.currentTimeMillis ());
				employee.setStatus (false);
				employee.setCompanyName ("空间秀");
				
				employeeDao.save (employee);		
			}
			else{
				
				throw new DataException("该员工已经在该部门存在！");
			}
		}
	}

	@Override
	public void deleteEmployee(long id)
	{
		
		employeeDao.delete (id);
	}

	@Override
	public void updateEmployee(Employee e)
	{
		
		Employee em = employeeDao.get (e.getId());
		if(ObjectUtils.notNull(em))
		{
			em.setName (e.getName ());
			em.setStatus (e.isStatus ());
			em.setSuperiorId (e.getSuperiorId ());
			em.setAge (e.getAge());
			em.setGender (e.getGender ());
			em.setMobile (e.getMobile ());
			em.setPhone (e.getPhone ());
			em.setEmail (e.getEmail ());
			em.setEnName (e.getEnName ());
			em.setLastAccess (e.getLastAccess ());
			em.setDepartmentId (e.getDepartmentId ());
			em.setAdmin (e.isIsAdmin  ());
			
			employeeDao.update (em);		
		}
		
		
	}

	@Override
	public Employee getEmployee(long id)
	{
		
		return employeeDao.get (id);
	}

	@Override
	public Page<Employee> queryEmployee(Map<String, Object> params, int currentPageNum,int maxPageRowCount)
	{
		
		Page<Employee> page = null; 

		if (ObjectUtils.isNull(params)) 
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = employeeDao.count(params);   

		if (count > 0)
		{
			page = new Page<Employee>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount); 
			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());
			List<Long> EmployeeIds = employeeDao.query(params);

			if (ObjectUtils.notEmpty(EmployeeIds))
			{
				List<Employee> Employees = new ArrayList<Employee>(EmployeeIds.size());

				for (long EmployeeId : EmployeeIds)
				{
					Employees.add(getEmployee (EmployeeId));
				}

				page.setDatas(Employees);
			}
		}

		return page;
	}
	
}
